package com.crdeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crdeng.business.entity.Product;
import com.crdeng.business.mapper.ProductMapper;
import com.crdeng.business.service.ProductCategoryService;
import com.crdeng.business.service.ProductService;
import com.crdeng.business.vo.ProductStockVO;
import com.crdeng.business.vo.ProductVO;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author crdeng
 * @since 2020-12-19
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired

    private ProductCategoryService productCategoryService;

    /**
     * 条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param productVO
     * @return
     */
    @Override
    public IPage<Product> getList(Integer pageNum, Integer pageSize, ProductVO productVO) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        //判断状态
        if (productVO.getStatus() != null) {
            queryWrapper.eq("status", productVO.getStatus());
        }
        //判断分类
        if (productVO.getThreeCategoryId() != null) {
            queryWrapper.eq("three_category_id", productVO.getThreeCategoryId());
            queryWrapper.eq("two_category_id", productVO.getTwoCategoryId());
            queryWrapper.eq("one_category_id", productVO.getOneCategoryId());
        } else if (productVO.getTwoCategoryId() != null) {
            queryWrapper.eq("two_category_id", productVO.getTwoCategoryId());
            queryWrapper.eq("one_category_id", productVO.getOneCategoryId());
        } else if (productVO.getOneCategoryId() != null) {
            queryWrapper.eq("one_category_id", productVO.getOneCategoryId());
        }
        //判断搜索条件
        if (!StringUtils.isEmpty(productVO.getName())) {
            queryWrapper.like("name", productVO.getName());
        }

        queryWrapper.ne("status",1);
        Page<Product> page = new Page<>(pageNum, pageSize);
        IPage<Product> selecPage = selecPage(page, queryWrapper);

        return selecPage;
    }

    /**
     * 分页
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Override
    public IPage<Product> selecPage(Page<Product> page, Wrapper<Product> queryWrapper) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * 添加
     *
     * @param productVO
     */
    @Override
    public void add(ProductVO productVO) {
        /*设置分类*/
        setCategory(productVO);

        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        Date date = new Date();
        product.setCreateTime(date);
        product.setModifiedTime(date);
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString().substring(0,32));
        //添加
        baseMapper.insert(product);

    }

    /**
     * 编辑物资资料信息
     * @param productVO
     */
    @Override
    public void edit(ProductVO productVO) {
        productVO.setPNum(null);
        Product byId = getById(productVO.getId());
        if (byId==null) throw  new  BusinessException(ResultCode.PRODYCT_NOT_EXIST.getCode(),ResultCode.PRODYCT_NOT_EXIST.getMessage());
        //设置分类
        setCategory(productVO);
        Product product = new Product();
        BeanUtils.copyProperties(productVO, product);
        //更新
        updateById(product);

    }

    /**
     * 修改资料的状态   是否删除:1 物资正常:0 物资回收:2 物资审核中:3
     * @param id
     * @param status
     */
    @Override
    public void alterStatus(Long id, Integer status) {


        Product product = getById(id);
        if (product==null||product.getStatus()==1){
            throw new BusinessException(ResultCode.PRODYCT_NOT_EXIST.getCode(),ResultCode.PRODYCT_NOT_EXIST.getMessage());
        }
        product.setStatus(status);
       this.updateById(product);


    }

    @Override
    public IPage<ProductStockVO> findProductStocks(Integer pageNum, Integer pageSize, ProductVO productVO) {

        //分页
        Page<ProductStockVO> productPage = new Page<>(pageNum,pageSize);
        //条件
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        //判断状态
        if (productVO.getStatus() != null) {
            queryWrapper.eq("p.status", productVO.getStatus());
        }
        //判断分类
        if (productVO.getThreeCategoryId() != null) {
            queryWrapper.eq("p.three_category_id", productVO.getThreeCategoryId());
            queryWrapper.eq("p.two_category_id", productVO.getTwoCategoryId());
            queryWrapper.eq("p.one_category_id", productVO.getOneCategoryId());
        } else if (productVO.getTwoCategoryId() != null) {
            queryWrapper.eq("p.two_category_id", productVO.getTwoCategoryId());
            queryWrapper.eq("p.one_category_id", productVO.getOneCategoryId());
        } else if (productVO.getOneCategoryId() != null) {
            queryWrapper.eq("p.one_category_id", productVO.getOneCategoryId());
        }
        //判断搜索条件
        if (!StringUtils.isEmpty(productVO.getName())) {
            queryWrapper.like("p.name", productVO.getName());
        }

        IPage<ProductStockVO> productStocks = baseMapper.findProductStocks(productPage, queryWrapper);

        return productStocks;
    }


    /**
     * 设置商品资料分类
     *
     * @param productVO
     */
    private void setCategory(ProductVO productVO) {
        int length = productVO.getCategoryKeys().length;
        if (length == 3) {
            List<Long> categoryId = productCategoryService.getCategoryId();
            ArrayList<Long> categoryList = new ArrayList<>();
            for (Long categoryKey : productVO.getCategoryKeys()) {
                categoryList.add(categoryKey);
            }
            boolean flag = categoryId.containsAll(categoryList);
            if (!flag) {
                throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(), ResultCode.PARAM_NOT_VALID.getMessage());
            }
            productVO.setOneCategoryId(productVO.getCategoryKeys()[0]);
            productVO.setTwoCategoryId(productVO.getCategoryKeys()[1]);
            productVO.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        } else {
            throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(), ResultCode.PARAM_NOT_VALID.getMessage());

        }
    }
}
